using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.SGI.ERP.Entidades;

namespace MakeIT.SGI.ERP.Negocios
{
	public partial class SgiDAOT : DAOT
	{

		public FormapgtoVO getFormapgto(int? idFormaPgto, string formaPgto)
		{
			FormapgtoVO formapgtoVO = null;
			List<FormapgtoVO> listFormapgtoVOs = getFormapgtos(idFormaPgto, formaPgto, null);
			if (listFormapgtoVOs != null && listFormapgtoVOs.Count > 0)
			{
				formapgtoVO = listFormapgtoVOs[0];
			}
			return formapgtoVO;
		}

		public FormapgtoVO getFormapgto(int? idFormaPgto)
		{
			return getFormapgto(idFormaPgto, null);
		}

		public List<FormapgtoVO> getFormapgtos()
		{
			return getFormapgtos(null, null, null);
		}

		public List<FormapgtoVO> getFormapgtos(int? idFormaPgto)
		{
			return getFormapgtos(idFormaPgto, null, null);
		}

		public List<FormapgtoVO> getFormapgtos(int? idFormaPgto, string formaPgto)
		{
			return getFormapgtos(idFormaPgto, formaPgto, null);
		}

		public List<FormapgtoVO> getFormapgtos(int? idFormaPgto, string formaPgto, int? idFilial)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT fpg.idFormaPgto as fpg_idFormaPgto, fpg.formaPgto as fpg_formaPgto, fpg.idFilial as fpg_idFilial, fil.filial as fil_filial, fpg.cadastro as fpg_cadastro, fpg.cadastradoPor as fpg_cadastradoPor, fpg.edicao as fpg_edicao, fpg.editadoPor as fpg_editadoPor  ");
			query.AppendLine("FROM formapgto as fpg LEFT OUTER JOIN filial as fil on fpg.idFilial=fil.idFilial  ");
			if (idFormaPgto != null)
			{
				query.AppendLine(where + "fpg.idFormaPgto=@idFormaPgto ");
				where = "AND ";
			}
			if (formaPgto != null)
			{
				query.AppendLine(where + "fpg.formaPgto LIKE @formaPgto ");
				where = "AND ";
			}
			if (idFilial != null)
			{
				query.AppendLine(where + "fpg.idFilial=@idFilial ");
				where = "AND ";
			}


			query.AppendLine("ORDER BY fpg.formaPgto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			if (idFormaPgto!= null) cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", idFormaPgto));
			if (formaPgto!= null) cmd.Parameters.Add(getParameter(cmd, "@formaPgto", "%"+formaPgto+"%"));
			if (idFilial!= null) cmd.Parameters.Add(getParameter(cmd, "@idFilial", idFilial));


			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<FormapgtoVO> listFormapgto = new List<FormapgtoVO>();

				while (reader.Read())
				{
					FormapgtoVO formapgtoVO = FormapgtoVO.CreateVO();
					formapgtoVO.idFormaPgto = reader.GetInt32(reader.GetOrdinal("fpg_idFormaPgto"));
					formapgtoVO.formaPgto = reader.GetString(reader.GetOrdinal("fpg_formaPgto"));
					formapgtoVO.idFilial = reader.GetInt32(reader.GetOrdinal("fpg_idFilial"));
					if (!reader.IsDBNull(reader.GetOrdinal("fil_filial")))
						formapgtoVO.idFilialVO.filial = reader.GetString(reader.GetOrdinal("fil_filial"));
					formapgtoVO.cadastro = reader.GetDateTime(reader.GetOrdinal("fpg_cadastro"));
					formapgtoVO.cadastradoPor = reader.GetString(reader.GetOrdinal("fpg_cadastradoPor"));
					formapgtoVO.edicao = reader.GetDateTime(reader.GetOrdinal("fpg_edicao"));
					formapgtoVO.editadoPor = reader.GetString(reader.GetOrdinal("fpg_editadoPor"));


					listFormapgto.Add(formapgtoVO);
				}
				reader.Close();
				return listFormapgto;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void setFormapgto(FormapgtoVO formapgtoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE formapgto ");
			query.AppendLine("SET formaPgto=@formaPgto, idFilial=@idFilial,  cadastro=@cadastro, cadastradoPor=@cadastradoPor, edicao=@edicao, editadoPor=@editadoPor ");
			query.AppendLine("WHERE idFormaPgto=@idFormaPgto ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", formapgtoVO.idFormaPgto));
			cmd.Parameters.Add(getParameter(cmd, "@formaPgto", formapgtoVO.formaPgto));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", formapgtoVO.idFilial));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", formapgtoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", formapgtoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", formapgtoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", formapgtoVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}

		public long newFormapgto(FormapgtoVO formapgtoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO formapgto ");
			query.AppendLine("  ( formaPgto, idFilial, cadastro, cadastradoPor, edicao, editadoPor ) ");
			query.AppendLine("VALUES ( @formaPgto, @idFilial, @cadastro, @cadastradoPor, @edicao, @editadoPor ) ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@formaPgto", formapgtoVO.formaPgto));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", formapgtoVO.idFilial));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", formapgtoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", formapgtoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", formapgtoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", formapgtoVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('formapgto') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }

		}

		public void delFormapgto(FormapgtoVO formapgtoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM formapgto ");
			query.AppendLine("WHERE idFormaPgto=@idFormaPgto ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idFormaPgto", formapgtoVO.idFormaPgto));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}
		public int getCountFormapgtoxIdFilial(int? idFilial)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFilial) as ocorrencias FROM formapgto WHERE IdFilial=@IdFilial ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFilial", idFilial));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
	}
}
